﻿using System;
using System.Windows;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.Platform.Core.ServiceModel;

namespace Baosight.ZNH.ZMCM.UI.ChildWindows
{
    /// <summary>
    /// Created by 李红星 2011-6-21
    /// 新增/修改材料费用额度管理
    /// </summary>
    public partial class InventoryDetail : EFViewCmes
    {
        public InventoryDetail(String dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);
        }
        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.InsertPanel.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="type"></param>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    //this.deptName.IsReadOnly = true;
                    base.Mode = OperationMode.Insert;
                    break;
                case "update":
                    //this.deptName.IsReadOnly = true;
                    //this.fdYear.IsReadOnly = true;
                    //this.fdMonth.IsEnabled = false;
                    this.fdMaterialCode.IsEnabled = false;
                    base.Mode = OperationMode.Update;
                    break;
                default:
                    break;
            }
        }
        #region customview 值获取
        /// <summary>
        /// Customview 取值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdMaterialCode_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFDataField field = sender as EFDataField;
            CmesCustomView picker = field.Content as CmesCustomView;
            if (picker.ItemObject != null &&(!picker.ItemObject.Equals("")))
            {
                if (!picker.ItemObject.Equals(""))
                {
                    this.fdMaterialId.Value = picker.ItemObject.GetType().GetProperty("fdKeyId").GetValue(picker.ItemObject, null);
                    this.fdMaterialName.Value = picker.ItemObject.GetType().GetProperty("fdMaterialName").GetValue(picker.ItemObject, null);
                    this.fdMaterialCode.Value = picker.ItemObject.GetType().GetProperty("fdMaterialCode").GetValue(picker.ItemObject, null);
                    this.fdQuality.Value = picker.ItemObject.GetType().GetProperty("fdQuality").GetValue(picker.ItemObject, null);
                    this.fdSpecModel.Value = picker.ItemObject.GetType().GetProperty("fdSpecModel").GetValue(picker.ItemObject, null);
                    this.fdNumber.Value = picker.ItemObject.GetType().GetProperty("fdNumber").GetValue(picker.ItemObject, null);
                    this.fdMaterialInnerCode.Value = picker.ItemObject.GetType().GetProperty("fdMaterialInnerCode").GetValue(picker.ItemObject, null);
                    this.fdPrice.Value = picker.ItemObject.GetType().GetProperty("fdPrice").GetValue(picker.ItemObject, null);
                    this.fdMetric.Value = picker.ItemObject.GetType().GetProperty("fdMetric").GetValue(picker.ItemObject, null);
                }
                else
                {
                    this.fdMaterialId.Value = "na";
                    this.fdMaterialName.Value = "";
                    this.fdMaterialCode.Value = "";
                    this.fdQuality.Value = "";
                    this.fdSpecModel.Value = "";
                    this.fdNumber.Value = "";
                    this.fdMaterialInnerCode.Value = "";
                    this.fdPrice.Value = "";
                    this.fdMetric.Value = "";
                }
                EIInfo eiInfo = new EIInfo();
                eiInfo.Set("service", "ZMCM51");
                eiInfo.Set("method", "getStockValue");
                eiInfo.Set("fdMaterialInnerCode", this.fdMaterialInnerCode.Value.ToString());
                eiInfo.Set("fdDepotId", this.fdDepotId.Value.ToString());

                HttpServiceAgent agent = new HttpServiceAgent();
                agent.CallService(eiInfo, CallServiceCompleted);
            }

            //throw new NotImplementedException();
        }
        #endregion

        /// <summary>
        /// 访问后台的回调
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CallServiceCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo == null)
            {
                base.OpenEFWindowAlert("操作失败！", true);
            }
            else
            {
                this.fdAccountSum.Value = e.EIInfo.Get("fdAccountSum");//将后台返回的账面库存量返回前台
                this.fdHasInvoiceNum.Value = e.EIInfo.Get("fdHasInvoiceNum");//将后台返回的账面有发票数量返回前台
                this.fdNoInvoiceNum.Value = e.EIInfo.Get("fdNoInvoiceNum");//将后台返回的账面有发票数量返回前台
                if (this.fdDepotId.Value.Equals("01"))
                {
                    this.fdHasInvoiceLoss.Value = e.EIInfo.Get("fdHasInvoiceNum");//将后台返回的账面有发票数量返回前台
                    this.fdNoInvoiceLoss.Value = e.EIInfo.Get("fdNoInvoiceNum");//将后台返回的账面无发票数量返回前台
                    this.fdActualSum.Value = e.EIInfo.Get("fdAccountSum");//将后台返回的账面库存量返回前台
                }
                
            }
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            if (!this.fdDepotId.Value.Equals("01"))
            {
                double sjk = 0, yfk = 0, wfk = 0;
                String sjkcl = (String)this.fdActualSum.Value;
                String yfpkcl = (String)this.fdHasInvoiceNum.Value;
                String wfpkcl = (String)this.fdNoInvoiceNum.Value;
                if (sjkcl != null && !sjkcl.Equals(""))
                {
                    sjk = Double.Parse(sjkcl);
                }
                if (yfpkcl != null && !yfpkcl.Equals(""))
                {
                    yfk = Double.Parse(yfpkcl);
                }
                if (wfpkcl != null && !wfpkcl.Equals(""))
                {
                    wfk = Double.Parse(wfpkcl);
                }
                if ((yfk + wfk) - sjk > 0)
                {
                    base.OpenEFWindowAlert("有发票库存量和无发票库存量总和大于实际库存量！", true);
                    return;
                }
            }
            base.OnCommitButtonClick("ZMCM51", sender, e);
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        private void fdActualSum_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            double zm=0,sj=0;
            this.fdActualSum.eRegex = @"((^\d{1,10}$)|(^\d{1,10}\.\d{0,5}$)|(^0\.\d{0,5}$))";
            this.fdActualSum.eValidateErrorPrompt = "输入整数位不超过10位，小数位不超过5位的数字";
            if (this.fdActualSum.Validate())
            {
                String zmkcl = (String)this.fdAccountSum.Value;
                String sjkcl = (String)this.fdActualSum.Value;
                if (!(zmkcl == null || zmkcl.Equals("")))
                {
                    zm = Double.Parse(zmkcl);
                }
                if (!(sjkcl == null || sjkcl.Equals("")))
                {
                    sj = Double.Parse(sjkcl);
                }
                this.fdProfitLoss.Value = sj - zm;
            }
        }

        private void fdAccountSum_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            double zm = 0, sj = 0;
            if (this.fdActualSum != null)
            {
                String zmkcl = (String)this.fdAccountSum.Value;
                String sjkcl = (String)this.fdActualSum.Value;
                if (!(zmkcl == null || zmkcl.Equals("")))
                {
                    zm = Double.Parse(zmkcl);
                }
                if (!(sjkcl == null || sjkcl.Equals("")))
                {
                    sj = Double.Parse(sjkcl);
                }
                this.fdProfitLoss.Value = sj - zm;
            }
        }
    }
}